<template>
  <div>
    <table border="1" width="700" style="border-collapse: collapse">
      <caption>购物车</caption>
      <thead>
        <tr>
          <th>
            <input type="checkbox" v-model="isShow"  @click="isShow"/>
            <span>全选</span>
          </th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <FormLi
          v-for="(item,index) in goodList"
          :key="index"
          :id="item.id"
          :index="index"
          :name="item.name"
          :price="item.price"
          :num="item.num"
          :checked="item.checked"
          :Dprice="item.price*item.num"
          @sub="sub"
          @add="add"
          @del1="del"
          @lll="lll"
        />
      </tbody>
      <tfoot>
        <tr>
          <td>合计:</td>
          <td colspan="5">{{sum}}</td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
import FormLi from './components/01.FormLi.vue'
export default {
  data() {
    return {
      goodList: [
        {
          id:1,
          name: '诸葛亮',
          price: 1000,
          num: 1,
          checked: false,
        },
        {
          id:2,
          name: '蔡文姬',
          price: 1500,
          num: 1,
          checked: false,
        },
        {
          id:3,
          name: '妲己',
          price: 2000,
          num: 1,
          checked: false,
        },
        {
          id:4,
          name: '鲁班',
          price: 2200,
          num: 1,
          checked: false,
        },
      ],
      isShow: false,
    }
  },
  components: {
    FormLi: FormLi,
  },
  methods: {
    sub(val, id) {
      this.goodList[id].num += val
      if (this.goodList[id].num< 0) {
        alert('不要再减了')
        this.goodList[id].num = 0
      }
    },
    add(val, id) {
      this.goodList[id].num += val
    },
    del(id) {
      //  const item = this.goodList.find(ele => ele.index == id)
      // console.log(ele.index)
      // this.goodList= this.goodList.filter(ele=> ele.index !== id)
      this.goodList.splice(id, 1)
    },
    // allAdd() {
    //   this.goodList.checked = !this.goodList.checked
    // },
    lll(e,id){
      // const arr=this.goodList.every(ele => ele.checked == val)
      // console.log(typeof val)
      // arr.checked= !arr.checked
      this.goodList.forEach(ele => {
        if(ele.id === id) {
          ele.checked = e
        }
      })
    }
  },
  computed: {
    sum() {
      return this.goodList.reduce((pre, ele) => {
        return pre + ele.num * ele.price
      }, 0)
    },
    isAll: {
      set(val) {
        this.arr.forEach((ele) => {
          ele.checked = val
        })
      },
      get() {
        return this.goodList.every((obj) => obj.checked === true)
      },
    },
  },
}
</script>

<style>
</style>